package com.etc.my.controller;


import com.etc.my.dto.MyMessage;
import com.etc.my.dto.OrderAndCarMessageDto;
import com.etc.my.dto.PageDto;
import com.etc.my.entity.User;
import com.etc.my.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
   private OrderService os;

    public OrderService getOs() {
        return os;
    }

    public void setOs(OrderService os) {
        this.os = os;
    }


    @RequestMapping("/getOrderByUserId")
    @ResponseBody
    public MyMessage getOrderByUserId(HttpSession session, Integer page, Integer size) {
        MyMessage message = new MyMessage();
        //获取登录的用户
        User user = (User) session.getAttribute("loginuser");



        if (user != null) {
            //判断是否有取到值
            if (page == null || page <= 0) {
                page = 1;
            }

            if (size == null || size <= 0) {
                size = 3;
            }
            //获取总条数
            Integer allCount = os.getOrderCountByUserId(user.getUser_id()).getAllCount();
            if (allCount >= 0 && allCount != null) {
                Integer maxPage = (allCount % size == 0) ? (allCount / size) : (allCount / size + 1);
                //页数条数上限容错
                page = page > maxPage ? maxPage : page;
                size = size > 3 ? 3 : size;
                //页数条数下限容错
                page = page <= 0 ? 1 : page;
                size = size <= 0 ? 3 : size;

                PageDto pd = new PageDto();
                pd.setPage(page);
                pd.setSize(size);

                //调用方法查询
                List<OrderAndCarMessageDto> pushList =os.getOrderByUserId(user.getUser_id(), (page - 1) * size, size);
                if (pushList.size() > 0 && pushList != null) {
                    message.setFlag(1);
                    pd.setMaxPage(maxPage);
                    pd.setAllCount(allCount);
                    pd.setOlist(pushList);
                    message.setData(pd);
                } else {
                    message.setFlag(0);
                    message.setInfo("查无此纪录");
                }
            } else {
                message.setFlag(0);
                message.setInfo("查无此纪录");
            }

        } else {
            message.setFlag(0);
            message.setInfo("您尚未登录请先登录!");
        }

        return  message;
    }

    @RequestMapping("/deleteOrderById")
    @ResponseBody
    public void deleteOrderById(Integer orderId, HttpServletResponse response) throws IOException {
           response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();

            if(os.deleteOrderById(orderId)){
                out.print("<script>alert('删除成功！');location.href='order.jsp'</script>");
            }else {
                out.print("<script>alert('删除失败！');history.go(-1)</script>");
            }

            out.flush();
            out.close();
    }
}
